package com.quanquan.store.controller;

import DTO.PageDTO;
import com.quanquan.store.dto.FloorDTO;
import com.quanquan.store.entity.Floor;
import com.quanquan.store.request.FloorRequest;
import com.quanquan.store.service.FloorService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import request.PageRequestBody;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;

/**
 * @ClassName:FloorController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/3/13 14:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/floor")
public class FloorController {

    @Autowired
    private FloorService floorService;

    /**
     * 添加楼层信息
     *
     * @param request
     * @return
     */
    @PostMapping("/add_floor")
    public ResultBody addFloor(@RequestBody FloorRequest request) {
        if (StringUtils.isEmpty(request.getFloorName())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "缺少楼层名称");
        }
//        if (StringUtils.isEmpty(request.getFloor())) {
//            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "缺少楼层名称");
//        }
        FloorDTO floorDTO = floorService.saveFloor(request);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", floorDTO);
    }

    /**
     * 查找楼层信息列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list")
    @ResponseBody
    public ResultBody queryList(@RequestBody FloorRequest request) {
        Integer page = request.getPage() == null ? 0 : request.getPage();
        Integer size = request.getSize() == null ? 100 : request.getSize();
        List<FloorDTO> floorDTOList = floorService.queryList(page, size);
        if (floorDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无业态信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", floorDTOList);
    }


    @PostMapping(value = "/query_management")
    @ResponseBody
    public ResultBody queryAllFloorsForManagement(@RequestBody PageRequestBody requestBody) {

        PageDTO<Floor> floorPageDTO = null;
        floorPageDTO = floorService.allFloorForManagement(requestBody);


        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), floorPageDTO);

    }


    /**
     * 删除id对应楼层信息
     *
     * @param request
     * @return
     */
    @PostMapping("/remove_floor")
    public ResultBody removeFloor(@RequestBody FloorRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明要删除的楼层id");
        }
        Integer mark = floorService.removeFloor(request.getId());
        if (mark == 1) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "删除楼层成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "操作失败");
        }
    }


    /**
     * 修改楼层信息
     *
     * @param request
     * @return
     */
    @PostMapping("update_floor")
    public ResultBody updateStore(@RequestBody FloorRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明id");
        }
        boolean flag = floorService.updateFloor(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "更新成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "更新失败");
        }
    }

    /**
     * 根据商户id查找楼层
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    @ResponseBody
    public ResultBody queryById(@RequestBody FloorRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "输入的楼层id为空");
        }
        FloorDTO floorDTO = floorService.query(request);
        if (floorDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此楼层信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", floorDTO);
    }

    @PostMapping("/floor_list")
    @ResponseBody
    public ResultBody floorList() {
        List<FloorDTO> floorDTOS = floorService.list();
        if (floorDTOS.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此楼层信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", floorDTOS);
    }

}
